import Axios from "axios";

export default {
    state: {
        // goodslist: [{
        //     brandId: "8bcb7027270c8082c5aaf2f41e151694",
        //     categoryId: "3aeddf3019dd08e0bb9457391e902032",
        //     fullname: "白云山 枸橼酸西地那非片 50mg*1s",
        //     id: "ed502e3b0bbcf7ab87c991db6074ac13",
        //     image: "http://image.360hwj.com/goods/920638/0b0bce24-1c29-4b69-9d8f-ee74cd2f12fd!400x400.jpg",
        //     marketPrice: "40.25",
        //     price: "35",
        //     sn: "1100014",
        //     type: "0",
        //     num: 2,
        //     cheaked: false
        // }, {
        //     brandId: "2fafd6fbeae01a6f081b0871f01bda52",
        //     categoryId: "1d6ebb67e6f8e9e3c04e7d26a9ec4585",
        //     fullname: "汇群堂 汇群堂黄连 5g",
        //     id: "18160a2fd6605c47ea5ee54bde84b883",
        //     image: "http://image.360hwj.com/goods/932058/42c9358f-5cc4-4696-992d-6ee2c144ab05!400x400.jpg",
        //     marketPrice: "1.76",
        //     price: "1.53",
        //     sn: "3011937",
        //     type: "0",
        //     num: 1,
        //     cheakrfalse
        // }
        // ]
        goodslist: []
    },
    getters: {
        total(state) {
            let [num, totalprice, num2] = [0, 0, 0];
            state.goodslist.forEach(item => {
                if (item.checked) {
                    num2++
                    num += item.num;
                    totalprice += item.price * item.num;
                }
            });
            return [num, totalprice, num2];
        }
    },
    mutations: {
        // del(state) {
        //     state.goodslist = state.goodslist.filter(item => !item.checked)
        // },
        clk(state, id) {
            // state.goodslist[index].checked = !state.goodslist[index].checked;
            state.goodslist.forEach(item => {
                if (item.id == id) {
                    item.checked = !item.checked;
                }
            })
        },
        add(state, id) {
            state.goodslist.forEach(item => {
                if (item.id == id) {
                    item.num = item.num + 1;
                }
            })
        }
    },
    actions: {
        async getGoodsList(context, {
            phonenumber
        }) {
            let {
                data: {
                    data
                }
            } = await Axios.get('http://localhost:1912/cart/cartlist', {
                params: {
                    phonenumber: phonenumber
                }
            });
            context.state.goodslist = data;
        }
    }

}